﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainFormalApplication : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private HrFormalApplicationEntity mSelectedFormalApp;
        public ApproveType ThisApproveType { get; set; }
        #endregion

        #region Constructor
        public frmMaintainFormalApplication(HrFormalApplicationEntity entity)
        {
            InitializeComponent();
            mSelectedFormalApp = entity;
        }
        public frmMaintainFormalApplication(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }
        public frmMaintainFormalApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetPersonFields()
        {
            txtPerson.Text = "";
            txtPersonNameCn.Text = "";
            cmbStatus.SelectedIndex = -1;
            txtDepartment.Text = "";
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPerson.Text = mSelectedPerson.PersonCode;
            txtPersonNameCn.Text = mSelectedPerson.NameCn;
            cmbStatus.SetSelectedValue(mSelectedPerson.Status);
            MstDepartmentEntity department = MstDepartmentBs.GetMstDepartmentByID(mSelectedPerson.DepartmentId);
            if (department != null)
            {
                txtDepartment.Text = department.DepartmentName;
            }
            MstPositionEntity position = MstPositionBs.GetMstPositionByID(mSelectedPerson.PositionId);
            if (position != null)
            {
                txtPosition.Text = position.PositionName;
            }
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FASTATUS", false);
        }

        private void SetFields()
        {
            if (mSelectedFormalApp == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedFormalApp.PersonId) == false)
            {
                mSelectedPerson = MstPersonBs.GetMstPersonByID(mSelectedFormalApp.PersonId);
                SetPersonFields();
            }
            if (mSelectedFormalApp.ApplicationDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedFormalApp.ApplicationDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpApplicationDate.EditValue = mSelectedFormalApp.ApplicationDate;
            }
            cmbStatus.SetSelectedValue(mSelectedFormalApp.Status);
            txtFormalReason.Text = mSelectedFormalApp.FormalReason;

            txtDmRemark.Text = mSelectedFormalApp.DmRemark;
            UICommonFunctions.SetTextBoxDate(txtDmApprovedDate, mSelectedFormalApp.DmApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtDmPerson, mSelectedFormalApp.DmPerson);
            txtHrRemark.Text = mSelectedFormalApp.HrRemark;
            UICommonFunctions.SetTextBoxDate(txtHrApprovedDate, mSelectedFormalApp.HrApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtHrPerson, mSelectedFormalApp.HrPerson);
            txtCeoRemark.Text = mSelectedFormalApp.CeoRemark;
            UICommonFunctions.SetTextBoxDate(txtCeoApprovedDate, mSelectedFormalApp.CeoApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCeoPerson, mSelectedFormalApp.CeoPerson);

            txtRemark.Text = mSelectedFormalApp.Remark;

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedFormalApp.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedFormalApp.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedFormalApp.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedFormalApp.LastModifiedBy);
        }

        private void SetEntity()
        {
            if (mSelectedFormalApp == null)
            {
                mSelectedFormalApp = new HrFormalApplicationEntity();
                mSelectedFormalApp.ApplicationId = Guid.NewGuid().ToString();
            }
            if (mSelectedPerson != null)
            {
                mSelectedFormalApp.PersonId = mSelectedPerson.PersonId;
                mSelectedFormalApp.DepartmentId = mSelectedPerson.DepartmentId;
                mSelectedFormalApp.PositionId = mSelectedPerson.PositionId;
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                mSelectedFormalApp.ApplicationDate = Convert.ToDateTime(dtpApplicationDate.EditValue);
            }
            else
            {
                mSelectedFormalApp.ApplicationDate = Convert.ToDateTime("1900-01-01");
            }
            mSelectedFormalApp.Status = "FASTATUS01";
            mSelectedFormalApp.FormalReason = txtFormalReason.Text;
            mSelectedFormalApp.Remark = txtRemark.Text;

            if (ThisActionType == ActionType.New)
            {
                mSelectedFormalApp.CreatedDate = DateTime.Now;
                mSelectedFormalApp.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                mSelectedFormalApp.LastModifiedDate = DateTime.Now;
                mSelectedFormalApp.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }

        private bool CheckData()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtFormalReason.Text))
            {
                MessageBox.Show("转正理由不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpApplicationDate.EditValue == null)
            {
                MessageBox.Show("申请日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = HrFormalApplicationBs.InsertHrFormalApplication(mSelectedFormalApp);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = HrFormalApplicationBs.UpdateHrFormalApplication(mSelectedFormalApp);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    switch (ThisApproveType)
                    {
                        case ApproveType.DM:
                            mSelectedFormalApp.DmApprovedDate = DateTime.Now;
                            mSelectedFormalApp.DmPerson = CommonParameter.CurrentUser.PersonId;
                            mSelectedFormalApp.DmRemark = txtDmRemark.Text;
                            break;
                        case ApproveType.HR:
                            mSelectedFormalApp.HrApprovedDate = DateTime.Now;
                            mSelectedFormalApp.HrPerson = CommonParameter.CurrentUser.PersonId;
                            mSelectedFormalApp.HrRemark = txtHrRemark.Text;
                            break;
                        case ApproveType.CEO:
                            mSelectedFormalApp.CeoApprovedDate = DateTime.Now;
                            mSelectedFormalApp.CeoPerson = CommonParameter.CurrentUser.PersonId;
                            mSelectedFormalApp.CeoRemark = txtCeoRemark.Text;
                            break;
                        default:
                            return false;
                    }
                    bool isSuccessful = HrFormalApplicationBs.ApproveHrFormalApplication(mSelectedFormalApp);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ResetControlsEnable()
        {
            txtDmRemark.Properties.ReadOnly = true;
            txtHrRemark.Properties.ReadOnly = true;
            txtCeoRemark.Properties.ReadOnly = true;
            if (ThisActionType == ActionType.Approve)
            {
                switch (ThisApproveType)
                {
                    case ApproveType.DM:
                        txtDmRemark.Properties.ReadOnly = false;
                        break;
                    case ApproveType.HR:
                        txtHrRemark.Properties.ReadOnly = false;
                        break;
                    case ApproveType.CEO:
                        txtCeoRemark.Properties.ReadOnly = false;
                        break;
                }
            }
        }
        #endregion

        #region Event
        private void frmMaintainFormalApplication_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetPersonFields();
            SetFields();
            ResetControlsEnable();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedFormalApp == null)
            {
                return;
            }
            switch (ThisApproveType)
            {
                case ApproveType.DM:
                    mSelectedFormalApp.Status = "FASTATUS02";
                    break;
                case ApproveType.HR:
                    mSelectedFormalApp.Status = "FASTATUS04";
                    break;
                case ApproveType.CEO:
                    mSelectedFormalApp.Status = "FASTATUS06";
                    break;
                default:
                    return;
            }
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Reject()
        {
            if (mSelectedFormalApp == null)
            {
                return;
            }
            switch (ThisApproveType)
            {
                case ApproveType.DM:
                    mSelectedFormalApp.Status = "FASTATUS03";
                    break;
                case ApproveType.HR:
                    mSelectedFormalApp.Status = "FASTATUS05";
                    break;
                case ApproveType.CEO:
                    mSelectedFormalApp.Status = "FASTATUS07";
                    break;
                default:
                    return;
            }
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                mSelectedPerson = frm.SelectedPerson;
                SetPersonFields();
            }
            else
            {
                mSelectedPerson = null;
                SetPersonFields();
            }
        }
        #endregion

    }
}
